####################### CREATE FUNCTIONS THAT MIGHT BE USEFUL LATER ###############################


getatts <- function() {
  atts <- att_gt(yname = "y", # name of the LHS variable
                 tname = "year", # name of the time variable
                 idname = "state_fips", # name of the id variable
                 gname = "treatyear", # name of the first treatment period variable
                 data = data, # name of the data
                 xformla =~dhpi,
                 est_method = "dr", # estimation method. "dr" means doubly robust
                 control_group = "notyettreated", # set the control group which is either "nevertreated" or "notyettreated" 
                 bstrap = TRUE, # if TRUE compute boostrapped SE
                 biters = 1000, # number of bootstrap iterations
                 print_details = FALSE, # if TRUE, print detailed results
                 clustervars = "state_fips", # cluster level
  ) 
}


getatts_nohpi <- function() {
  atts <- att_gt(yname = "y", # name of the LHS variable
                 tname = "year", # name of the time variable
                 idname = "state_fips", # name of the id variable
                 gname = "treatyear", # name of the first treatment period variable
                 data = data, # name of the data
                 xformla = NULL, # name of covariates
                 est_method = "dr", # estimation method. "dr" means doubly robust
                 control_group = "notyettreated", # set the control group which is either "nevertreated" or "notyettreated" 
                 bstrap = TRUE, # if TRUE compute boostrapped SE
                 biters = 1000, # number of bootstrap iterations
                 print_details = FALSE, # if TRUE, print detailed results
                 clustervars = "state_fips", # cluster level
  ) 
}



getatts_small <- function() {
  atts <- att_gt(yname = "y", # name of the LHS variable
                 tname = "year", # name of the time variable
                 idname = "state_fips", # name of the id variable
                 gname = "treatyear", # name of the first treatment period variable
                 data = data, # name of the data
                 xformla = NULL, # name of covariates
                 #xformla =~dhpi,
                 est_method = "dr", # estimation method. "dr" means doubly robust
                 control_group = "notyettreated", # set the control group which is either "nevertreated" or "notyettreated" 
                 bstrap = TRUE, # if TRUE compute boostrapped SE
                 biters = 1000, # number of bootstrap iterations
                 print_details = FALSE, # if TRUE, print detailed results
                 clustervars = "state_fips", # cluster level
                 panel = TRUE,
                 allow_unbalanced_panel = TRUE,
  ) 
}


getatts_small2 <- function() {
  atts <- att_gt(yname = "y", # name of the LHS variable
                 tname = "year", # name of the time variable
                 idname = "state_fips", # name of the id variable
                 gname = "treatyear", # name of the first treatment period variable
                 data = data, # name of the data
                 xformla = ~small + dhpi, # name of covariates
                 #xformla =~dhpi,
                 est_method = "dr", # estimation method. "dr" means doubly robust
                 control_group = "notyettreated", # set the control group which is either "nevertreated" or "notyettreated" 
                 bstrap = TRUE, # if TRUE compute boostrapped SE
                 biters = 1000, # number of bootstrap iterations
                 print_details = FALSE, # if TRUE, print detailed results
                 clustervars = "state_fips", # cluster level
  ) 
}




attcoef <- function() {
  t=agg_effects$overall.att/agg_effects$overall.se
  if (!is.na(agg_effects$overall.att)) {
    if (abs(t) >= qnorm(.995)) {
      beta <- paste0(round(agg_effects$overall.att, 4), "***") 
    }  else if (abs(t) < qnorm(.995) & abs(t) >= qnorm(.975)) { 
      beta <- paste0(round(agg_effects$overall.att, 4), "**") 
    }  else if (abs(t) < qnorm(.975) & abs(t) >= qnorm(.95))  {
      beta <- paste0(round(agg_effects$overall.att, 4), "*") 
    } else {
      beta <- round(agg_effects$overall.att, 4)
    }
  } else {
    beta = "NA"
  }  
  
  se <- paste0("=\"(", round(agg_effects$overall.se, 4), ")\"")
  elas <- round(agg_effects$overall.att/mean(data$y), 4)
  elas <- paste0("[", round(elas, 4), "]")
  df <- data.frame(Col1=c(label, "", "", ""), Col2=c(beta,se, elas, ""))
}


plotcses <- function(save) {
  #Create ylimit for scaling
  data_scale = as.data.frame(cbind(agg_effects_es$egt, agg_effects_es$att.egt, agg_effects_es$se.egt))
  tval = agg_effects_es$crit.val.egt
  tval = ifelse(is.na(tval), 2, tval)
  data_scale = data_scale %>% filter(V1 >= xmin & V1 <=xmax) %>% mutate(min = V2 - tval*V3) %>% mutate(max = V2 + tval*V3) 
  ymin = min(data_scale$min)
  ymax = max(data_scale$max)
  
  ymin=ifelse(ymin>=0, -1 * ymax/100, ymin)
  ymax=ifelse(ymax <=0, -1*ymin/100, ymax)
  ymin=ymin*1.25
  ymax=ymax*1.25
  
  #Edit Xscale 
  if (min(agg_effects_es$egt) > xmin) {xmin = min(agg_effects_es$egt)}
  if (max(agg_effects_es$egt) < xmax) {xmax = max(agg_effects_es$egt)}
  
  # Plot event-study coefficients
  plot <-  ggdid(agg_effects_es, xgap=3) + 
    theme_classic() +
    theme(legend.position = "none") + 
    labs(x="Years Since Prominent Minimum Wage Increase", y="Estimated Effect of Prominent Minimum Wage Increase") +
    geom_hline(yintercept =0, linetype="dashed", color="red") +
    geom_vline(xintercept =-0.5, linetype="dashed", color="red") +
    geom_line() +  geom_point(size=2.5) + 
    geom_errorbar(width=0.4, position=position_dodge(width=4), color="grey20") + 
    scale_x_continuous(limits=c(xmin-.3, xmax+.3,1), breaks = seq(xmin,xmax, by=1)) +
    ylim(ymin, ymax) +
    labs(title="", col="")  
  ggsave(filename=paste0("cs_estimates/figure/es_", save, ".png"), plot=plot)
  
}


plotcses_auto <- function(save) {
  
  #Edit Xscale 
  if (min(agg_effects_es$egt) > xmin) {xmin = min(agg_effects_es$egt)}
  if (max(agg_effects_es$egt) < xmax) {xmax = max(agg_effects_es$egt)}
  
  
  # Plot event-study coefficients
  plot <-  ggdid(agg_effects_es, xgap=3) + 
    theme_classic() +
    theme(legend.position = "none") + 
    labs(x="Years Since Prominent Minimum Wage Increase", y="Estimated Effect of Prominent Minimum Wage Increase") +
    geom_hline(yintercept =0, linetype="dashed", color="red") +
    geom_vline(xintercept =-0.5, linetype="dashed", color="red") +
    geom_line() +  geom_point(size=2.5) + 
    geom_errorbar(width=0.4, position=position_dodge(width=4), color="grey20") + 
    labs(title="", col="")
  ggsave(filename=paste0("cs_estimates/figure/es_", save, ".png"), plot=plot)
  
}


plotcses_manual <- function(save) {
  #Edit Xscale 
  if (min(agg_effects_es$egt) > xmin) {xmin = min(agg_effects_es$egt)}
  if (max(agg_effects_es$egt) < xmax) {xmax = max(agg_effects_es$egt)}
  
  # Plot event-study coefficients
  plot <-  ggdid(agg_effects_es, xgap=3) + 
    theme_classic() +
    theme(legend.position = "none") + 
    labs(x="Years Since Prominent Minimum Wage Increase", y="Estimated Effect of Prominent Minimum Wage Increase") +
    geom_hline(yintercept =0, linetype="dashed", color="red") +
    geom_vline(xintercept =-0.5, linetype="dashed", color="red") +
    geom_line() +  geom_point(size=2.5) + 
    geom_errorbar(width=0.4, position=position_dodge(width=4), color="grey20") + 
    scale_x_continuous(limits=c(xmin-.3, xmax+.3,1), breaks = seq(xmin,xmax, by=1)) +
    ylim(ymin, ymax) +
    labs(title="", col="")  
  ggsave(filename=paste0("cs_estimates/figure/es_", save, ".png"), plot=plot, width=6.5, height=4)
  
}


plotcses_manual_2 <- function(save) {
  #Edit Xscale 
  if (min(agg_effects_es$egt) > xmin) {xmin = min(agg_effects_es$egt)}
  if (max(agg_effects_es$egt) < xmax) {xmax = max(agg_effects_es$egt)}
  
  # Plot event-study coefficients
  plot <-  ggdid(agg_effects_es, xgap=3) + 
    theme_classic() +
    theme(legend.position = "none") + 
    labs(x="Years Since Prominent Minimum Wage Increase", y="Estimated Effect of Prominent Minimum Wage Increase") +
    geom_hline(yintercept =0, linetype="dashed", color="red") +
    geom_vline(xintercept =-0.5, linetype="dashed", color="red") +
    geom_line() +  geom_point(size=2.5) + 
    geom_errorbar(width=0.8, position=position_dodge(width=4), color="grey20") + 
    scale_x_continuous(limits=c(xmin-.4, xmax+.4,7), breaks = seq(xmin,xmax, by=1)) +
    ylim(ymin, ymax) +
    labs(title="", col="")  
  ggsave(filename=paste0("cs_estimates/figure/es_", save, ".png"), plot=plot, width=6.5, height=4)
  
}